package com.runvp.handler;

import com.runvp.common.core.redis.DistributeRedisLock;
import com.runvp.common.core.redis.RedisCache;
import com.runvp.common.core.redis.RedisLockClient;
import com.runvp.common.enums.NettyCmd;
import com.runvp.common.model.tcp.MessageResBody;
import com.runvp.netty.build.BuildStaticBean;
import com.runvp.netty.client.build.BuildServerReqBodyFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SendInsideCmdHandler {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private RedisLockClient lockClient;

    public static SendInsideCmdHandler instance() {
        return new SendInsideCmdHandler();
    }

    public SendInsideCmdHandler() {
    }



    /**
     * 内部推送消息
     * @param cmd
     * @param sn
     * @param body
     */
    public boolean send(NettyCmd cmd,String sn,String body){
        log.info("本次执行命令:{}", cmd.getDesc());
        DistributeRedisLock redisLock = lockClient.getRedisLock("outLock:"+sn);
        try {
            redisLock.lock();
            MessageResBody resBody =  new BuildServerReqBodyFactory(true,cmd,sn,body).build();
            boolean res = BuildStaticBean.baseHandle.sendMsg(sn,resBody);
            log.info("内部推送状态:{}",res);
            return res;
        }finally {
            redisLock.unlock();
        }
    }
}
